package com.usefullc.system.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.usefullc.system.entity.WebResult;
import com.usefullc.system.service.MessagePublishService;
import com.usefullc.system.service.SerialNumberService;
import com.usefullc.system.service.component.scheduler.QuartzJobComponent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * @author: Sampson
 * @date: 2022-01-10 15:28
 */
@RestController
@RequestMapping(path = "/system/test/")
@Api( tags="测试操作管理 ")
public class TestController extends BaseController {

    @Autowired
    private MessagePublishService initDataService;

    @Autowired
    private SerialNumberService serialNumberService;

    @Autowired
    private QuartzJobComponent quartzJobComponent;


    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "测试序号生成", response = WebResult.class)
    @GetMapping(path = "testSerialNumber", produces = "application/json")
    public WebResult testSerialNumber(@RequestParam String tableName, @RequestParam String type) {
        String no = serialNumberService.getSerialNumber(tableName, type);
        return WebResultBuilder.successResult(no);
    }

    @ApiOperationSupport(author = "Sampson@usefullc.com")
    @ApiOperation(value = "测试定时任务", response = WebResult.class)
    @GetMapping(path = "testJob", produces = "application/json")
    public WebResult testJob(@RequestParam String op,@RequestParam String key,@RequestParam(required = false) String cronExpression) {

        if(op.equalsIgnoreCase("start")){
            Map<String,String> paramMap = new TreeMap<>();
            paramMap.put(key,cronExpression);
            quartzJobComponent.start(paramMap);
        }else if(op.equalsIgnoreCase("add")){
            quartzJobComponent.add(key,cronExpression);
        }else if(op.equalsIgnoreCase("update")){
            quartzJobComponent.update(key,cronExpression);
        }else if(op.equalsIgnoreCase("del")){
            quartzJobComponent.remove(key);
        }

//        paramMap.put("b1","0 */1 * * * ?");
        return WebResultBuilder.successResult();
    }
}
